package edu.tongji.gridding.thread;

import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import edu.tongji.gridding.datamodel.Response;
import edu.tongji.gridding.datamodel.Result;


@SuppressWarnings("unchecked")
public class TaskThread extends Thread{
	private String methodName;
	private String[] keywords;
	private QueryManager queryManager;
	private Response response;
	private String rMethodName;
	private CountDownLatch CDL;
	
	
	public TaskThread(QueryManager queryManager, String methodName, String[] keywords, Response response, String rMethodName, CountDownLatch CDL) {
		this.queryManager = queryManager;
		this.methodName = methodName;
		this.keywords = keywords;
		this.response = response;
		this.rMethodName = rMethodName;
		this.CDL = CDL;
	}
	
	@Override
	public void run() {
		try {
			Method method = QueryManager.class.getMethod(methodName, String[].class);
			List<Result> results = (List<Result>)method.invoke(queryManager, (Object)keywords);
			Method rmethod = Response.class.getMethod(rMethodName, List.class);
			rmethod.invoke(response, results);
			CDL.countDown();
		} catch (Exception e) {
			System.out.println(e);
		}
		
		
	}

}
